package com.yufei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yufei.common.R;
import com.yufei.entity.Employee;
import com.yufei.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * @author zyf
 * @desc 关于用户模块的接口
 */
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工登录
     */
    @PostMapping("/employee/login")
    public R<Employee> empLogin(HttpServletRequest request,@RequestBody Employee employee){
        System.out.println("接收到的数据，用户名："  + employee.getUsername() + ",密码" + employee.getPassword());

        //构造条件
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username", employee.getUsername());
        //对密码加密后再查数据库
        String md5Pass = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        wrapper.eq("password", md5Pass);
        //查询
        Employee employee1 = employeeService.getOne(wrapper);

        if(employee1 == null){
            return R.error("用户名或密码为错误");
        }

        if(employee1.getStatus() == 0){
            return R.error("账户已禁用");
        }
        //将登录信息存储到session
        request.getSession().setAttribute("employee", employee1.getId());
        return R.success(employee1);
    }
    /**
     * 分页 + 模糊查询全部员工
     */
    @GetMapping("/employee/page")
    public R<Page<Employee>> page(Integer pageSize, Integer page,Employee employee){
       //分页对象
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        String name = employee.getName();
        //如果模糊搜索名为空就查全表，不拼接
        //不为空就拼接上去查询
        wrapper.like(StringUtils.hasText(name),"name", employee.getName());

        pageInfo = employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }
    /**
     * 员工退出
     */
    @PostMapping("/employee/logout")
    public R<String> logout(HttpServletRequest request){
        //清空session
        request.getSession().removeAttribute("employee");
        return R.success("正在退出");
    }

    /**
     * 添加员工
     * @param request 用于获取session
     * @param employee 员工信息
     * @return 保存成功
     */
    @PostMapping("/employee")
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword("123456");
        String md5Pass = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        employee.setPassword(md5Pass);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        //从session中拿到当前登陆的用户id，并放到CreateUser中
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.save(employee);
        return R.success("保存成功");
    }

    /**
     * 编辑用户回显
     * @param id
     * @return
     */
    @GetMapping("/employee/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     * 编辑用户
     * @param employee 用户信息
     * @return
     */
    @PutMapping("/employee")
    public R<String> editEmployee(@RequestBody Employee employee,HttpServletRequest request){
        /**
         * 先判断是不是禁用管理员
         */
        String username = employeeService.getById(employee.getId()).getUsername();
        Integer status = employee.getStatus();
        if(username.equals("admin") && status == 0){
            return R.error("管理员不能被禁用");
        }
        Long employee1 = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(new Date());
        employee.setUpdateUser(employee1);
        boolean update = employeeService.updateById(employee);
        if(update) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
